import { combineResolvers } from 'graphql-resolvers'
import auth from '../../middlewares/auth'

module.exports = {
  Mutation: {
    enqueueNotification: combineResolvers(
      auth,
      async (root, { formId }, { services: { $notification }, user }) => {
        console.info('enqueue notification:', formId, user.id)
        return await $notification.create(formId, user.id)
      }
    ),
    dequeueNotification: combineResolvers(
      auth,
      async (root, args, { services: { $notification }, user }) => {
        let userId = user.id
        console.info('dequeue notification:', userId)
        return await $notification.findAndRemove(userId)
      }
    ),
    sendNotification: async (
      root,
      arg,
      { services: { $notification }, user }
    ) => {
      let {
        data: { title, data, url },
        target: { userId, targetId, scope }
      } = arg
      console.info('send notification:', arg)
      return await $notification.sendNotification(
        { title, data, url },
        { userId, targetId, scope }
      )
    }
  }
}
